<template><div><h2 id="mongodb" tabindex="-1"><a class="header-anchor" href="#mongodb"><span>MongoDB</span></a></h2>
<p>Yii2 框架的官方扩展 <code v-pre>yii2-mongodb</code>，为我们提供了 <a href="https://www.mongodb.com/" target="_blank" rel="noopener noreferrer">MongoDB</a> 数据库相关功能的整合。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>这个扩展要求 您的 PHP 安装了 <a href="http://us1.php.net/manual/zh/set.mongodb.php" target="_blank" rel="noopener noreferrer">MongoDB 扩展</a>，且版本要求 &gt;= 1.0.0。同时也要求 MongoDB 服务端版本 &gt;= 3.0。</p>
<p>安装此扩展的首选方式是通过 <a href="http://getcomposer.org/download/" target="_blank" rel="noopener noreferrer">composer</a>，命令如下：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require yiisoft/yii2-mongodb</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者在您的 <code v-pre>composer.json</code> 文件中的 require 部分增加以下内容：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token property">"yiisoft/yii2-mongodb"</span><span class="token operator">:</span> <span class="token string">"~2.1.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后执行 <code v-pre>composer install</code> 命令即可。</p>
<h2 id="配置应用" tabindex="-1"><a class="header-anchor" href="#配置应用"><span>配置应用</span></a></h2>
<p>要使用这个扩展，只需在您的应用程序配置中添加以下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mongodb'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'\yii\mongodb\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mongodb://localhost:27017/mydatabase'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string double-quoted-string">"username"</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"Username"</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string double-quoted-string">"password"</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"Password"</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">//以上dsn与options，可以直接这样写</span></span>
<span class="line">            <span class="token comment">//'dsn' => 'mongodb://Username:Password@localhost:27017/mydatabase',</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<p>Once you have a MongoDB connection instance, you can execute a MongoDB commands and queries using [[yii\mongodb\Command]]:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// execute command:</span></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'listIndexes'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'some_collection'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// execute query (find):</span></span>
<span class="line"><span class="token variable">$cursor</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'projection'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">query</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'some_collection'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// execute batch (bulk) operations:</span></span>
<span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addInsert</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'new'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addUpdate</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'existing'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'updated'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addDelete</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'old'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">executeBatch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'customer'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Using the connection instance you may access databases and collections. Most of the MongoDB commands are accessible via [[\yii\mongodb\Collection]] instance:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">getCollection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'customer'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$collection</span><span class="token operator">-></span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Smith'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>To perform “find” queries, you should use [[\yii\mongodb\Query]]:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// compose the query</span></span>
<span class="line"><span class="token variable">$query</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'status'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'customer'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// execute the query</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="mongoid-specifics" tabindex="-1"><a class="header-anchor" href="#mongoid-specifics"><span>MongoId specifics</span></a></h2>
<h3 id="getting-scalar-from-document-id" tabindex="-1"><a class="header-anchor" href="#getting-scalar-from-document-id"><span>Getting scalar from document ID</span></a></h3>
<p>Remember: MongoDB document id (<code v-pre>_id</code> field) is not scalar, but an instance of [[\MongoDB\BSON\ObjectID]] class. To get actual Mongo ID string your should typecast [[\MongoDB\BSON\ObjectID]] instance to string:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$row</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'customer'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">one</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// outputs: "object(\MongoDB\BSON\ObjectID)"</span></span>
<span class="line"><span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// outputs "string 'acdfgdacdhcbdafa'"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Although this fact is very useful sometimes, it often produces some problems. You may face them in URL composition or attempt of saving “_id” to other storage. In these cases, ensure you have converted [[\MongoDB\BSON\ObjectID]] into the string:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">/* @var $this yii\web\View */</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createUrl</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'item/update'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'_id'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>Same applies to implementing user identity which is stored in MongoDB. When implementing [[\yii\web\IdentityInterface::getId()]] you should typecast [[\MongoDB\BSON\ObjectID]] class to scalar in order for authentication to work.</p>
<h3 id="getting-document-id-from-scalar" tabindex="-1"><a class="header-anchor" href="#getting-document-id-from-scalar"><span>Getting document ID from scalar</span></a></h3>
<p>While building condition, values for the key ‘_id’ will be automatically cast to [[\MongoDB\BSON\ObjectID]] instance, even if they are plain strings. So it is not necessary for you to perform back cast of string ‘_id’ representation:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ItemController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@param</span> <span class="token class-name"><span class="token keyword">string</span></span> <span class="token parameter">$id</span> MongoId string (not object)</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">actionUpdate</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$row</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'item'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'_id'</span> <span class="token operator">=></span> <span class="token variable">$id</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// implicit typecast to [[\MongoDB\BSON\ObjectID]]</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">one</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token operator">...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>However, if you have other columns, containing [[\MongoDB\BSON\ObjectID]], you should take care of possible typecast on your own.</p>
<h2 id="使用-mongodb-activerecord" tabindex="-1"><a class="header-anchor" href="#使用-mongodb-activerecord"><span>使用 MongoDB ActiveRecord</span></a></h2>
<p>This extension provides ActiveRecord solution similar ot the [[\yii\db\ActiveRecord]]. To declare an ActiveRecord class you need to extend [[\yii\mongodb\ActiveRecord]] and implement the <code v-pre>collectionName</code> and ‘attributes’ methods:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>ActiveRecord</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Customer</span> <span class="token keyword">extends</span> <span class="token class-name">ActiveRecord</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span> the name of the index associated with this ActiveRecord class.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">collectionName</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'customer'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span> list of attribute names.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'status'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Note: collection primary key name (‘_id’) should be always explicitly setup as an attribute.</p>
<p>You can use [[\yii\data\ActiveDataProvider]] with [[\yii\mongodb\Query]] and [[\yii\mongodb\ActiveQuery]]:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>data<span class="token punctuation">\</span>ActiveDataProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'customer'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$provider</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveDataProvider</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'query'</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'pagination'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'pageSize'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$models</span> <span class="token operator">=</span> <span class="token variable">$provider</span><span class="token operator">-></span><span class="token function">getModels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>data<span class="token punctuation">\</span>ActiveDataProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">app<span class="token punctuation">\</span>models<span class="token punctuation">\</span>Customer</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$provider</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveDataProvider</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'query'</span> <span class="token operator">=></span> <span class="token class-name static-context">Customer</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'pagination'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'pageSize'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$models</span> <span class="token operator">=</span> <span class="token variable">$provider</span><span class="token operator">-></span><span class="token function">getModels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用嵌入式文档" tabindex="-1"><a class="header-anchor" href="#使用嵌入式文档"><span>使用嵌入式文档</span></a></h2>
<p>This extension does not provide any special way to work with embedded documents (sub-documents) at the moment. General recommendation is avoiding it if possible. For example: instead of:</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    content<span class="token operator">:</span> <span class="token string">"some content"</span><span class="token punctuation">,</span></span>
<span class="line">    author<span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        name<span class="token operator">:</span> author1<span class="token punctuation">,</span></span>
<span class="line">        email<span class="token operator">:</span> author1@domain.com</span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>use following:</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    content<span class="token operator">:</span> <span class="token string">"some content"</span><span class="token punctuation">,</span></span>
<span class="line">    author_name<span class="token operator">:</span> author1<span class="token punctuation">,</span></span>
<span class="line">    author_email<span class="token operator">:</span> author1@domain.com</span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Yii Model designed assuming single attribute is a scalar. Validation and attribute processing based on this suggestion. Still any attribute can be an array of any depth and complexity, however you should handle its validation on your own.</p>
<p>While there is no explicit support for embedded documents, there is also no explicit restriction on it. You may create your own solution or use third-party extension like <a href="https://github.com/yii2tech/embedded" target="_blank" rel="noopener noreferrer">yii2tech/embedded</a> for this feature.</p>
<h2 id="聚合" tabindex="-1"><a class="header-anchor" href="#聚合"><span>聚合</span></a></h2>
<p>This extension provides support for the <a href="https://docs.mongodb.com/manual/aggregation/" target="_blank" rel="noopener noreferrer">MongoDB aggregation functionality</a> wrapping corresponding commands into PHP methods of [[\yii\mongodb\Command]].</p>
<h3 id="single-purpose-aggregation-operations" tabindex="-1"><a class="header-anchor" href="#single-purpose-aggregation-operations"><span>Single Purpose Aggregation Operations</span></a></h3>
<p>The simplest MongoDB aggregation operations are <code v-pre>count</code> and <code v-pre>distinct</code>, which are available via [[\yii\mongodb\Command::count()]] and [[\yii\mongodb\Command::distinct()]] correspondingly. For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$booksCount</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">count</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'category'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'programming'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>You may as well use [[\yii\mongodb\Collection::count()]] and [[\yii\mongodb\Collection::distinct()]] shortcut methods:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$booksCount</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">getCollection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'category'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'programming'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Methods <code v-pre>count()</code> and <code v-pre>distinct()</code> are also available at [[\yii\mongodb\Query]] class:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$booksCount</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'category'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'programming'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="pipeline" tabindex="-1"><a class="header-anchor" href="#pipeline"><span>Pipeline</span></a></h3>
<p><a href="https://docs.mongodb.com/manual/core/aggregation-pipeline/" target="_blank" rel="noopener noreferrer">Aggregation Pipeline</a> can be executed via [[\yii\mongodb\Command::aggregate()]]. The following example display how you can group books by <code v-pre>authorId</code> field:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$authors</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">aggregate</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'$group'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'$authorId'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You may as well use [[\yii\mongodb\Collection::aggregate()]] as shortcut. In the following example we are grouping books by both <code v-pre>authorId</code> and <code v-pre>category</code> fields:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">getCollection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$authors</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-></span><span class="token function">aggregate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'$group'</span>   <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'_id'</span>      <span class="token operator">=></span> <span class="token string single-quoted-string">'$authorId'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'category'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'$category'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Multiple pipelines can be specified for more sophisticated aggregation. In the following example we are grouping books by <code v-pre>authorId</code> field, sorting them by <code v-pre>createdAt</code> field descending and then we are limiting the result to 100 documents skipping first 300 records.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">getCollection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$authors</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-></span><span class="token function">aggregate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'$match'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'$ne'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'$group'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'$authorId'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'$sort'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'createdAt'</span> <span class="token operator">=></span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'$skip'</span> <span class="token operator">=></span> <span class="token number">300</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'$limit'</span> <span class="token operator">=></span> <span class="token number">100</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Please refer to <a href="https://docs.mongodb.com/manual/core/aggregation-pipeline/" target="_blank" rel="noopener noreferrer">MongoDB Aggregation Pipeline Docs</a> for detailed information about pipeline specifications.</p>
<h3 id="aggregation-via-yii-mongodb-query" tabindex="-1"><a class="header-anchor" href="#aggregation-via-yii-mongodb-query"><span>Aggregation via [[\yii\mongodb\Query]]</span></a></h3>
<p>Simple aggregations can be performed via following methods of the [[\yii\mongodb\Query]] class:</p>
<ul>
<li><code v-pre>sum()</code> - returns the sum of the specified column values.</li>
<li><code v-pre>average()</code> - returns the average of the specified column values.</li>
<li><code v-pre>min()</code> - returns the minimum of the specified column values.</li>
<li><code v-pre>max()</code> - returns the maximum of the specified column values.</li>
</ul>
<p>In case of these methods invocation [[\yii\mongodb\Query::$where]] will be used for <code v-pre>$match</code> pipeline composition.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$maxPrice</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'$ne'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">max</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token variable">$db</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="map-reduce" tabindex="-1"><a class="header-anchor" href="#map-reduce"><span>Map Reduce</span></a></h3>
<p><a href="https://docs.mongodb.com/manual/core/map-reduce/" target="_blank" rel="noopener noreferrer">Map Reduce</a> can be executed via [[\yii\mongodb\Command::mapReduce()]].</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">createCommand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mapReduce</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'function () {emit(this.status, this.amount)}'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'function (key, values) {return Array.sum(values)}'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'mapReduceOut'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'$lt'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You may as well use [[\yii\mongodb\Collection::mapReduce()]] as shortcut.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">getCollection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'books'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mapReduce</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'function () {emit(this.status, this.amount)}'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'function (key, values) {return Array.sum(values)}'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'mapReduceOut'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'$lt'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用-gridfs" tabindex="-1"><a class="header-anchor" href="#使用-gridfs"><span>使用 GridFS</span></a></h2>
<p>This extension supports <a href="https://docs.mongodb.com/manual/core/gridfs/" target="_blank" rel="noopener noreferrer">MongoGridFS</a> via classes under namespace “\yii\mongodb\file”. There you will find specific Collection, Query and ActiveRecord classes.</p>
<p>You can upload a file using [[\yii\mongodb\file\Upload]]:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$document</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">getFileCollection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">createUpload</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addContent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Part 1'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">addContent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Part 2'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">complete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You can download the file using [[\yii\mongodb\file\Download]]:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">getFileCollection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">createDownload</span><span class="token punctuation">(</span><span class="token variable">$document</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file.dat'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Each row of the file query result contains [[\yii\mongodb\file\Download]] instance at the key ‘file’:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>file<span class="token punctuation">\</span>Query</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fs'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$rows</span> <span class="token keyword">as</span> <span class="token variable">$row</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'file'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// outputs: "object(\yii\mongodb\file\Download)"</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'file'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// outputs file content</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Using [\yii\mongodb\file\ActiveRecord]] you can manipulate the file using ‘file’ property:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>mongodb<span class="token punctuation">\</span>file<span class="token punctuation">\</span>ActiveRecord</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ImageFile</span> <span class="token keyword">extends</span> <span class="token class-name">ActiveRecord</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$record</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ImageFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$record</span><span class="token operator">-></span><span class="token property">number</span> <span class="token operator">=</span> <span class="token number">15</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$record</span><span class="token operator">-></span><span class="token property">file</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/path/to/some/file.jpg'</span><span class="token punctuation">;</span> <span class="token comment">// upload local file to GridFS</span></span>
<span class="line"><span class="token variable">$record</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$record</span> <span class="token operator">=</span> <span class="token class-name static-context">ImageFile</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'number'</span> <span class="token operator">=></span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">one</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$record</span><span class="token operator">-></span><span class="token property">file</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// outputs: "object(\yii\mongodb\file\Download)"</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$record</span><span class="token operator">-></span><span class="token property">file</span><span class="token operator">-></span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// outputs file content</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You may as well operate GridFS files via regular PHP stream resource. You will need to register a stream wrapper provided by this extension - [[\yii\mongodb\file\StreamWrapper]]. This can be done via [[\yii\mongodb\file\Connection::registerFileStreamWrapper()]]. Once stream wrapper is registered, you may open a stream resource using following format:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'protocol://databaseName.fileCollectionPrefix?file_attribute=value'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">mongodb</span><span class="token operator">-></span><span class="token function">registerFileStreamWrapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// register stream wrapper</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// write a file:</span></span>
<span class="line"><span class="token variable">$resource</span> <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'gridfs://mydatabase.fs?filename=new_file.txt'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'w'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">fwrite</span><span class="token punctuation">(</span><span class="token variable">$resource</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'some content'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"><span class="token function">fclose</span><span class="token punctuation">(</span><span class="token variable">$resource</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// write file with several fields:</span></span>
<span class="line"><span class="token variable">$resource</span> <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'gridfs://mydatabase.fs?filename=new_file.txt&amp;number=17&amp;status=active'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'w'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">fwrite</span><span class="token punctuation">(</span><span class="token variable">$resource</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'file number 17 with status "active"'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">fclose</span><span class="token punctuation">(</span><span class="token variable">$resource</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// read a file:</span></span>
<span class="line"><span class="token variable">$resource</span> <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'gridfs://mydatabase.fs?filename=my_file.txt'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'r'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$fileContent</span> <span class="token operator">=</span> <span class="token function">stream_get_contents</span><span class="token punctuation">(</span><span class="token variable">$resource</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="日志和分析" tabindex="-1"><a class="header-anchor" href="#日志和分析"><span>日志和分析</span></a></h2>
<p>This extension provides logging for executed commands or queries. Logging is optional and can be enabled or disabled at [[\yii\mongodb\Connection]] level:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mongodb'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'\yii\mongodb\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mongodb://developer:password@localhost:27017/mydatabase'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'enableLogging'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// enable logging</span></span>
<span class="line">            <span class="token string single-quoted-string">'enableProfiling'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// enable profiling</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: log messages generated for the commands and queries do not contain actual text of the performed queries, they contains only a <strong>close approximation</strong> of it, composed on the values which can be extracted from PHP MongoDB extension classes. If you need to see actual query text, you should use specific tools for that.</p>
</blockquote>
<blockquote>
<p>Tip: Keep in mind that composition of the log messages take some time and program resources. Thus it make sense to disable logging at the production environment.</p>
</blockquote>
<h2 id="使用-cache-组件" tabindex="-1"><a class="header-anchor" href="#使用-cache-组件"><span>使用 Cache 组件</span></a></h2>
<p>To use the <code v-pre>Cache</code> component, in addition to configuring the connection as described in <a href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/installation.md" target="_blank" rel="noopener noreferrer">Installation</a> section, you also have to configure the <code v-pre>cache</code> component to be <code v-pre>yii\mongodb\Cache</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\mongodb\Cache'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用-session-组件" tabindex="-1"><a class="header-anchor" href="#使用-session-组件"><span>使用 Session 组件</span></a></h2>
<p>To use the <code v-pre>Session</code> component, in addition to configuring the connection as described in <a href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/installation.md" target="_blank" rel="noopener noreferrer">Installation</a> section, you also have to configure the <code v-pre>session</code> component to be <code v-pre>yii\mongodb\Session</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'session'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\mongodb\Session'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用-i18n-消息源" tabindex="-1"><a class="header-anchor" href="#使用-i18n-消息源"><span>使用 I18N 消息源</span></a></h2>
<p>You may use [[\yii\mongodb\i18n\MongoDbMessageSource]] for the i18n message translations storage. Application configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'i18n'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'translations'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'*'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\mongodb\i18n\MongoDbMessageSource'</span></span>
<span class="line">                <span class="token punctuation">]</span></span>
<span class="line">            <span class="token punctuation">]</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>[[\yii\mongodb\i18n\MongoDbMessageSource]] uses single collection to store all translations. Each entry in this collection should have 3 fields:</p>
<ul>
<li>language: string, translation language</li>
<li>category: string, name translation category</li>
<li>messages: array, list of actual message translations, in each element: the ‘message’ key is raw message name and ‘translation’ key - message translation.</li>
</ul>
<p>For example:</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"app"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"language"</span><span class="token operator">:</span> <span class="token string">"de"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"messages"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"Hello world!"</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token property">"translation"</span><span class="token operator">:</span> <span class="token string">"Hallo Welt!"</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"The dog runs fast."</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token property">"translation"</span><span class="token operator">:</span> <span class="token string">"Der Hund rennt schnell."</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        ...</span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You also can specify ‘messages’ using source message as a direct BSON key, while its value holds the translation. For example:</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"category"</span><span class="token operator">:</span> <span class="token string">"app"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"language"</span><span class="token operator">:</span> <span class="token string">"de"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"messages"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"Hello world!"</span><span class="token operator">:</span> <span class="token string">"Hallo Welt!"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"See more"</span><span class="token operator">:</span> <span class="token string">"Mehr sehen"</span><span class="token punctuation">,</span></span>
<span class="line">        ...</span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>However such approach is not recommended as BSON keys can not contain symbols like <code v-pre>.</code> or <code v-pre>$</code>.</p>
<p>Please refer to [[\yii\mongodb\i18n\MongoDbMessageSource]] for more details about configuration and translation collection data structure.</p>
<h2 id="使用-gii-generator" tabindex="-1"><a class="header-anchor" href="#使用-gii-generator"><span>使用 Gii generator</span></a></h2>
<p>This extension provides a code generator, which can be integrated with yii ‘gii’ module. It allows generation of the Active Record code. In order to enable it, you should adjust your application configuration in following way:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'modules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'gii'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\gii\Module'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'generators'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'mongoDbModel'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\mongodb\gii\model\Generator'</span></span>
<span class="line">                <span class="token punctuation">]</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: since MongoDB is schemaless, there is not much information, which generated code may base on. So generated code is very basic and definitely requires adjustments.</p>
</blockquote>
<h2 id="使用-mongodb-debug-面板" tabindex="-1"><a class="header-anchor" href="#使用-mongodb-debug-面板"><span>使用 MongoDB Debug 面板</span></a></h2>
<p>The yii2 MongoDB extensions provides a debug panel that can be integrated with the yii debug module and shows the executed MongoDB queries.</p>
<p>Add the following to you application config to enable it (if you already have the debug module enabled, it is sufficient to just add the panels configuration):</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'debug'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'modules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'debug'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\\debug\\Module'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'panels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'mongodb'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\\mongodb\\debug\\MongoDbPanel'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token comment">// 'db' => 'mongodb', // MongoDB component ID, defaults to `db`. Uncomment and change this line, if you registered MongoDB component with a different ID.</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用数据库迁移" tabindex="-1"><a class="header-anchor" href="#使用数据库迁移"><span>使用数据库迁移</span></a></h2>
<p>MongoDB is schemaless and will create any missing collection on the first demand. However there are many cases, when you may need applying persistent changes to the MongoDB database. For example: you may need to create a collection with some specific options or create indexes. MongoDB migrations are managed via [[yii\mongodb\console\controllers\MigrateController]], which is an analog of regular [[\yii\console\controllers\MigrateController]].</p>
<p>In order to enable this command you should adjust the configuration of your console application:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'controllerMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mongodb-migrate'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\mongodb\console\controllers\MigrateController'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Below are some common usages of this command:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># creates a new migration named 'create_user_collection'</span></span>
<span class="line">yii mongodb-migrate/create create_user_collection</span>
<span class="line"></span>
<span class="line"><span class="token comment"># applies ALL new migrations</span></span>
<span class="line">yii mongodb-migrate</span>
<span class="line"></span>
<span class="line"><span class="token comment"># reverts the last applied migration</span></span>
<span class="line">yii mongodb-migrate/down</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="special-configuration-for-an-application-that-uses-more-than-one-db-engine" tabindex="-1"><a class="header-anchor" href="#special-configuration-for-an-application-that-uses-more-than-one-db-engine"><span>Special configuration for an application that uses more than one DB engine</span></a></h3>
<p>In case your application uses multiple databases, example:</p>
<ul>
<li>MySQL + MongoDB</li>
</ul>
<p>If you run the migration commands, it will evaluate both MySQL and MongoDB migration files at the same time since both by default share the same folder.</p>
<p><strong>Problem: MongoDB will try to run MySQL’s migration files and the other way around.</strong></p>
<p>In order to avoid that behavior, you can create a new folder called <code v-pre>mongodb</code> under your <code v-pre>migrations</code> folder, and then setup your console application like this:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'controllerMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mongodb-migrate'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">          <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\mongodb\console\controllers\MigrateController'</span><span class="token punctuation">,</span></span>
<span class="line">          <span class="token string single-quoted-string">'migrationPath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@app/migrations/mongodb'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


